/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: tc_utils.c
    Module  : Common module.

        This file includes the operations of extended utilities.

=======================================================================================================================
Date               Name              Description of Change
14-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
#ifndef _cplusplus
#define _CRT_SECURE_NO_DEPRECATE
#endif


#ifdef BUILD_IMAN
#include <iman_string.h>
#include <iman_util.h>

#define GTC_strcmp          iman_strcmp
#define GTC_strstr          iman_strstr

#else
#include <fclasses\tc_string.h>
#include <fclasses\tc_date.h>

#define GTC_strcmp          tc_strcmp
#define GTC_strstr          tc_strstr
#endif

#include <unidefs.h>
#include <itk/mem.h>
#include <time.h>
#include <pom\pom\pom.h>
#include "tc_utils.h"
#include "string_utils.h"

logical GUTIL_exist_str( const char *text, const Vector_t *textList )
{
    int size  = 0, i = 0;
    char *vecText = NULL;

    if (text == NULL || textList == NULL)
        return false;

    size = VECTOR_size( textList );
    for (i = 0; i < size; i++)
    {
        vecText = (char *)VECTOR_get( textList, i );
        if (vecText != NULL && GTC_strcmp(text, vecText) == 0 )
        {
            return true;
        }
    }
    return false;
}

logical GUTIL_exist_tag( tag_t tag, const Vector_t *tagList )
{
    int size  = 0, i = 0;
    tag_t *vecTag = NULLTAG;

    if (tag == NULLTAG || tagList == NULL)
        return false;

    size = VECTOR_size( tagList );
    for (i = 0; i < size; i++)
    {
        vecTag = (tag_t*)VECTOR_get( tagList, i );
        if (*vecTag != NULLTAG && *vecTag == tag )
        {
            return true;
        }
    }
    return false;
}


logical GUTIL_is_begin_strs( const char *text, const Vector_t *textList )
{
    int size  = 0, i = 0;
    char *vecText = NULL;

    if (text == NULL || textList == NULL)
        return false;

    size = VECTOR_size( textList );
    for (i = 0; i < size; i++)
    {
        vecText = (char *)VECTOR_get( textList, i );

        if ( GUTIL_is_begin_str(text, vecText, false) == true )
            return true;
    }
    return false;
}

logical GUTIL_is_begin_str( const char *text, const char *b_str, logical iscase )
{
    logical isBegin = false;
    int size  = 0, i = 0;
    char *vecText = NULL;

    if (text == NULL || b_str == NULL)
        return false;

    if (iscase)
    {
        char *temp_str = NULL, *temp_b_str = NULL;
        GSTR_clone( &temp_str, text );
        GSTR_clone( &temp_b_str, b_str );
        STRNG_to_lower( temp_str );
        STRNG_to_lower( temp_b_str );

        isBegin = (GTC_strstr(temp_str, temp_b_str) == temp_str);
        MEM_free( temp_str );
        MEM_free( temp_b_str );
    }
    else
    {
        isBegin = (GTC_strstr(text, b_str) == text);
    }
    return isBegin;
}


void GUTIL_time_to_date( time_t time, date_t *date )
{
    struct tm *oldDate = localtime( &time );
    date->year = 1900 + oldDate->tm_year;
    date->month = oldDate->tm_mon;
    date->day = oldDate->tm_mday;
    date->hour = oldDate->tm_hour;
    date->minute = oldDate->tm_min;
    date->second = oldDate->tm_sec;
}
